/*
 * @Descripttion: 
 * @Author: whyjs
 * @Date: 2022-05-07 21:59:53
 * @LastEditors: whyjs
 * @LastEditTime: 2023-02-16 19:16:33
 */
//验证码
var _CaptchaKey = ''
//校验
var validtaeFn = function (val, parentNode, msg, reg) {
  //正则为空，只校验是否为空
  if (!reg) {
    if (val == "") {
      if ($("#" + parentNode + " .error_tips").length > 0) {
        $("#" + parentNode + " .error_tips").text(msg);
      } else {
        var error_tips = '<div class="error_tips">' + msg + '</div>';
        $("#" + parentNode).append(error_tips);
      }
      return false;
    } else {
      $("#" + parentNode + " .error_tips").remove();
      return true;
    }
  } else {
    if (val == "") {
      if ($("#" + parentNode + " .error_tips").length > 0) {
        $("#" + parentNode + " .error_tips").text('请输入' + msg);
      } else {
        var error_tips = '<div class="error_tips">请输入' + msg + '</div>';
        $("#" + parentNode).append(error_tips);
      }
      return false;
    } else {
      var regNew = new RegExp(reg);
      if (regNew.test(val)) {
        $("#" + parentNode + " .error_tips").remove();
        return true
      } else {
        if ($("#" + parentNode + " .error_tips").length > 0) {
          $("#" + parentNode + " .error_tips").text(msg + '格式不正确');
        } else {
          var error_tips = '<div class="error_tips">' + msg + '格式不正确</div>';
          $("#" + parentNode).append(error_tips);
        }
        return false;
      }
    }
  };
};
//远程验证添加错误提示
var addErrorTips = function(parentNode, msg){
  if ($("#" + parentNode + " .error_tips").length > 0) {
    $("#" + parentNode + " .error_tips").text(msg);
  } else {
    var error_tips = '<div class="error_tips">' + msg + '</div>';
    $("#" + parentNode).append(error_tips);
  }
  if(msg==''){
    $("#" + parentNode + " .error_tips").remove()
  }
};

//keyUp之后检查当前的输入，是否正确
var checkInputFn = function (val) {
  switch (val) {
    case "companyNameVal":
      var companyNameVal = $("#companyNameVal").val();
      console.log("请输入正确的企业名称.....");
      validtaeFn(companyNameVal, "companyName", "请输入正确的企业名称");
      break;
    case "companyCardVal":
      var companyCardVal = $("#companyCardVal").val();
      var companyCardReg = /^[A-Z0-9]{18}$/i
      validtaeFn(companyCardVal, "companyCard", "统一社会信用代码",companyCardReg);
      break;
    case "userNameVal":
      var userNameVal = $("#userNameVal").val();
      var userReg = /^[a-zA-Z0-9]+$/i;
      validtaeFn(userNameVal, "userName", "用户名",userReg);
      break;
    case "userPwdAgainVal":
      var userPwdAgainVal = $("#userPwdAgainVal").val();
      validtaeFn(userPwdAgainVal, "userPwdAgain", "请再次输入密码");
      break;
    case "userMobileVal":
      var userMobileVal = $("#userMobileVal").val();
      validtaeFn(userMobileVal, "userMobile", "请输入手机号码");
      break;
    case "photoCodeVal":
      var photoCodeVal = $("#photoCodeVal").val();
      validtaeFn(photoCodeVal, "photoCode", "请输入图形验证码");
      break;
    case "smsCodeVal":
      var smsCodeVal = $("#smsCodeVal").val();
      validtaeFn(smsCodeVal, "smsCode", "请输入手机验证码");
      break;
    default:
      break;
  }
  function1();
};
var function1 = function(){
  if (($("#userPwdAgainVal").val() != $("#userPwdVal").val())&&$("#userPwdVal").val()) {
    if ($("#" + "userPwdAgain" + " .error_tips").length > 0) {
      $("#" + "userPwdAgain" + " .error_tips").text("两次输入密码不一致");
    } else {
      var error_tips = '<div class="error_tips">两次输入密码不一致</div>';
      $("#" + "userPwdAgain").append(error_tips);
    }
  }
}
//获取短信验证码
var isSendSms = true;
var getVcode = function () {
  if( !isSendSms ){
    return;
  }
  var n = 0;
  var userMobileVal = $("#userMobileVal").val();
  var reg = /^1\d{10}$/;
  !validtaeFn(userMobileVal, "userMobile", "手机号码", reg) ? n++ : '';

  //图形验证码
  var photoCodeVal = $("#photoCodeVal").val();
  !validtaeFn(photoCodeVal, "photoCode", "请输入图形验证码") ? n++ : '';
  //console.log(n)
  if (n == 0) {
    var params = {
      phone: userMobileVal,
      valcode: photoCodeVal,
      kaptchaKey:getCookie("kaptchaKey")||'',
    }
    fnBase.ajaxJson({
      type:"get",
      url:"/goldnet-enterprise-client/comm/send/valcode",
      headers:{
        "Captcha-Key":_CaptchaKey,
        "Captcha-Code":photoCodeVal,
      },
      data:params,
      success:function(res){
        //console.log(res);
        layer.msg("发送成功");
        timeDownFn(60);
        isSendSms = false;
      },
      error:function(res){

      }
    })
  }
};
//倒计时
var timeDownFn=function(n){
  var m = n-1;
  setTimeout(function(){
    if( m == 0   ){
      $("#get_code").html("重新获取验证码");
      isSendSms = true;
    }else{
      $("#get_code").html(m+"秒后重新获取");
      timeDownFn(m);
    }
  },1000)
}
//密码强度
var pwdstrength = function () {
  //密码校验
  // var userPwdVal = $("#userPwdVal").val();
  // validtaeFn(userPwdVal, "userPwd", "请输入密码");
  var pwdVal = $("#userPwdVal").val();
  //密码强度正则
  var strongRegex = new RegExp("^(?=.{3,})(?=.*[A-Za-z])(?=.*[0-9])(?=.*\\W).*$", "g");
  var mediumRegex = new RegExp("^(?=.{3,})(((?=.*[A-Z])(?=.*[a-z]))|((?=.*[A-Z])(?=.*[0-9]))|((?=.*[a-z])(?=.*[0-9]))).*$", "g");
  var enoughRegex = new RegExp("(?=.{3,}).*", "g");
  if (strongRegex.test(pwdVal)) {
    //密码为字母数字特殊字符三项都包括,强度最强 
    $(".pwd_strength_main .left").css({"background":"#FF6C6C", "color":"#262626"});
    $(".pwd_strength_main .center").css({"background":"#F29E38", "color":"#262626"});
    $(".pwd_strength_main .right").css({"background":"#9CF31B", "color":"#262626"});
  }
  else if (mediumRegex.test(pwdVal)) {
    //密码为字母、数字、特殊字符三项中有两项，强度是中等 
    $(".pwd_strength_main .left").css({"background":"#FF6C6C", "color":"#262626"});
    $(".pwd_strength_main .center").css({"background":"#FB8333", "color":"#262626"});
    $(".pwd_strength_main .right").css({"background":"#EBEFF4"});
  } else if (enoughRegex.test(pwdVal)) {
    //只有一种字母时显示为低 
    $(".pwd_strength_main .left").css({"background":"#FF6C6C", "color":"#262626"});
    $(".pwd_strength_main .center").css({"background":"#EBEFF4"});
    $(".pwd_strength_main .right").css({"background":"#EBEFF4"});
  } else {
    //默认为弱
    $(".pwd_strength_main .left").css({"background":"#FF6C6C", "color":"#262626"});
    $(".pwd_strength_main .center").css({"background":"#EBEFF4"});
    $(".pwd_strength_main .right").css({"background":"#EBEFF4"});
  }
};
//提交注册
var btn = true;
var submitFn = function () {
  var isSubmit = true;
  //企业名称
  var companyNameVal = $("#companyNameVal").val();
  isSubmit = validtaeFn(companyNameVal, "companyName", "请输入正确的企业名称");
  //企业信用代码
  var companyCardVal = $("#companyCardVal").val();
  var companyCardReg = /^[A-Z0-9]{18}$/i
  isSubmit = validtaeFn(companyCardVal, "companyCard", "统一社会信用代码",companyCardReg);
  //用户名
  var userNameVal = $("#userNameVal").val();
  var userReg = /^[a-zA-Z0-9]+$/i;
  isSubmit = validtaeFn(userNameVal, "userName", "用户名",userReg);
  var userNewReg = /^(?!\d+$)[\da-zA-Z]+$/
  if(!userNewReg.test(userNameVal)){
    addErrorTips("userName","账号支持输入数字和字母（必须包含字母）,3-15字符");
    isSubmit = false
  }
  //注册角色
  var identityVal = $("input[name='identity']:checked").val();
  //console.log(identityVal);
  //账号密码
  var userPwdVal = $("#userPwdVal").val();
  // isSubmit = validtaeFn(userPwdVal, "userPwd", "请输入密码");
  // var reg = /^(?!.*\s)(?=.*[A-Z])(?=.*[a-z])(?=.*[0-9])(?=.*[\W_]).{8,16}$/
  // isSubmit = validtaeFn(userPwdVal, "userPwd","账号密码", reg);
  userPwdValBlur()
  //确认密码
  var userPwdAgainVal = $("#userPwdAgainVal").val();
  if (userPwdAgainVal) {
    var parentNode = "userPwdAgain";
    if (userPwdAgainVal != userPwdVal) {
      if ($("#" + parentNode + " .error_tips").length > 0) {
        $("#" + parentNode + " .error_tips").text("两次输入密码不一致");
      } else {
        var error_tips = '<div class="error_tips">两次输入密码不一致</div>';
        $("#" + parentNode).append(error_tips);
      }
    }
  } else {
    isSubmit = validtaeFn(userPwdAgainVal, "userPwdAgain", "请再次输入密码");
  }
  //手机号码
  var userMobileVal = $("#userMobileVal").val();
  var reg = /^1\d{10}$/;
  isSubmit = validtaeFn(userMobileVal, "userMobile", "手机号码", reg);
  //图形验证码
  var photoCodeVal = $("#photoCodeVal").val();
  //isSubmit = validtaeFn(photoCodeVal, "photoCode", "请输入图形验证码");
  //短信验证码
  var smsCodeVal = $("#smsCodeVal").val();
  var smsReg = /^\d{6}$/;
  //isSubmit = validtaeFn(smsCodeVal, "smsCode", "手机验证码", smsReg);

  var n = $(".reg_main .error_tips").length;
  if (n == 0) {
    if( !btn  ){
      return;
    }
    btn = false;
    var uri = "/gongyinglian/RegistController";
    var params = {
      name: companyNameVal,//企业名称
      socialCreditCode: companyCardVal,//统一社会信用代码必输
      entNo: userNameVal,//用户账号
      role: identityVal,//台用户类型必输 1核心2银行3链条
      password: userPwdVal,//密码
      phoneNo: userMobileVal,//手机号
      captchaCode: photoCodeVal,//图形验证码
      phoneValNumber: smsCodeVal,//手机验证码
    }
    console.log("zhuce")
    $.ajax({
      "type":"post",
      "dataType":"json",
      "url":"/gongyinglian/RegistController",
      "data":params,
      success:function(res){
          alert("注册成功")
          window.location.href = "/gongyinglian/login.jsp";
      },
      error:function (err) {
          //提交失败处理
          alert("注册失败")
      }
    })
    
  }
};
//校验用户名是否存在
var checkUserIsExist = function () {
  var userNameVal = $("#userNameVal").val();
  //用户存在，不为空，去校验，是否存在
  var reg = /^((?![^a-zA-Z]+$)|(?![a-zA-Z0-9]+$)).{3,15}$/
  
  // var reg = /(^[a-zA-Z]$|^[a-zA-Z0-9]){}$/
  if(!reg.test(userNameVal)){
    addErrorTips("userName","账号支持输入数字和字母（必须包含字母）,3-15字符");
    return
  }
  if (userNameVal) {
    var params = {
      account:userNameVal,
    }
    $.ajax({
      "type":"get",
      "dataType":"json",
      "url":"/gongyinglian/CheckNameController",
      "data":params,
      success:function(res){
        console.log(res)
        if( res.code != 200 ){
          //console.log("账号已存在")
          addErrorTips("userName","账号已存在");
        }
      },
      error:function(err){

      }
    })
  }
};
//校验企业用户名和信用号
var checkCompanyInfo = function () {
  console.log("check");
  //企业名称
  var companyNameVal = $("#companyNameVal").val();
  //企业信用代码
  var companyCardVal = $("#companyCardVal").val();
  var reg = /^[\w\u4E00-\u9FA5（）()-.·、]{2,50}$/
  // addErrorTips("companyName","企业名称可输入“中文  数字  字母（）-  ”，长度2-50字符");
  if(!reg.test(companyNameVal)){
    addErrorTips("companyName","企业名称可输入“中文  数字  字母（）-  ”，、.长度2-50字符");
    return
  }
  //同时不为空的时候，去请求后台接口，校验企业名称和信用代码
 // if (companyNameVal && companyNameVal && companyNameVal != "" && companyCardVal != "") {
  if (companyNameVal &&  companyNameVal != "" ) {
    var uri = "/gongyinglian/CheckNameController";
    var params = {
      "enterpriseName": companyNameVal,
    }
    fnBase.ajaxJson({
      type:"get",
      url:uri,
      data:params,
      success:function(res){
        if (res.code == 200) {
          //企业名称已存在
          if( res.data == 1 ){
            addErrorTips("companyName","当前企业名称已注册，请勿重复注册");
          }
          //企业信用代码已存在
          // if( !res.data.socialCreditCode ){
          //   addErrorTips("companyCard","企业信用代码已存在");
          // }
        }
      },
      error:function(err){
       
      }
    })
  }
};
//刷新验证码
var reloadCodeImg = function () {
  // var kaptchaKeyUrl = $("#kaptchaKeyId").attr("data-src");
  // var newUri = kaptchaKeyUrl + "&time=" + new Date().getTime();
  //var uri = fnBase.baseUrl+"/goldnet-auth/oauth/captcha";///////////////
  
//  $.get(uri,function(res){
//    // setCookie("Captcha-Key",res.key)
//    _CaptchaKey = res.key
//    $("#kaptchaKeyId").attr("src", res.image);
//  })


  
};
reloadCodeImg();
var userPwdValBlur = function(){
  var reg = /^(?!.*\s)(?=.*[A-Z])(?=.*[a-z])(?=.*[0-9])(?=.*[\W_]).{8,16}$/
  var regNew = /^(?=.*[a-z])(?=.*[A-Z])(?=.*[A-Z])(?=.*\d)(?=.*[!.@#$%^&*()\-+])(?!.*(.)\1{2})(?!.*(?:012|123|234|345|456|567|678|789))[a-zA-Z0-9!.@#$%^&*()\-+]{8,}$/
  var userPwdVal = $('#userPwdVal').val()
  var account = $("#userNameVal").val()
  if(!reg.test(userPwdVal)){
    addErrorTips('userPwd','密码必须是8-16位，需同时包含大写字母、小写字母、数字、特殊字符4种');
    return
  }else if(!regNew.test(userPwdVal)){
    addErrorTips('userPwd','密码中不能包含3个及以上连续的数字或连续相同的字符');
    return
  }else if(fnBase.similar(account,userPwdVal)>80){
    addErrorTips('userPwd','密码与账号高度相似，请修改');
    return
  }else{
    addErrorTips("userPwd","");
  }
}
var checkCompanyCardVal = function(){
  var reg = /[A-Z0-9]{18}/g
  var companyCardVal = $('#companyCardVal').val()
  if(!reg.test(companyCardVal)){
    addErrorTips("companyCard","统一社会信用代码校验位数18位，仅支持输入数字和大写字母");
  }else{
    addErrorTips("companyCard","");
  }
}

$("#smsCodeVal").keydown(function (e) {
  if (e.keyCode == 13) submitFn()
});


$(document).ready(function () {
  if($(window).height()>900){
    $(".reg_backgroud").css({'min-height':$(window).height()-120 + 'px'})
  }
  $(window).resize(function() {
    if($(window).height()>900){
      $(".reg_backgroud").css({'min-height':$(window).height()-120 + 'px'})
    }
  });
});